//
//  CNGameUtil.m
//  chuiniu
//
//  Created by zheng ji on 14-9-29.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "CNGameUtil.h"
#import "CNGoldCoin.h"
@implementation CNGameUtil
+(CNPlayer*) getCurrentPlayerFromUserDefaults{
    NSUserDefaults* _userDefaults=[NSUserDefaults standardUserDefaults];
    NSData* playerData=[_userDefaults objectForKey:@"player"];
    if(playerData !=nil){
        CNPlayer* p=[NSKeyedUnarchiver unarchiveObjectWithData:playerData];
        return p;
    }
    return nil;
}

+(void)saveCurrentPlayerToUserDefaults:(CNPlayer*) player{
     NSUserDefaults* _userDefaults=[NSUserDefaults standardUserDefaults];
    NSData *udObject = [NSKeyedArchiver archivedDataWithRootObject:player];
    [_userDefaults setObject:udObject forKey:@"player"];
    
    [_userDefaults synchronize];
}
//存放本地金币记录的路径
+ (NSString *) getLocalGoldPath{
    return [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/goldfiles"];
}
+ (NSString *)CreateGUID
{
    CFUUIDRef    uuidObj = CFUUIDCreate(nil);//create a new UUID
    
    //get the string representation of the UUID
    
    NSString    *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj);
    
    CFRelease(uuidObj);
    　　return uuidString ;
}

+(void) writeContentToFile:(NSString*)content filePath:(NSString*) filePath{
    
    NSData* data= [content dataUsingEncoding:NSUTF8StringEncoding];
    
    [data writeToFile:filePath atomically:YES];
    
    
    //[NSData dataWithContentsOfFile:filePath]
}
+(NSString*) readContentFromFile:(NSString*)filePath{
    NSData* data=[NSData dataWithContentsOfFile:filePath];
    return [[NSString alloc]initWithData:data
                                encoding:NSUTF8StringEncoding];
}


/*将金币记录保存到本地*/
+(void) writeGoldContentToFile:(NSString*)content goldFileName:(NSString*) goldFileName{
     NSString* localFile=[NSString stringWithFormat:@"%@/%@",  [CNGameUtil getLocalGoldPath],goldFileName];
   // [NSThread detachNewThreadSelector:<#(SEL)#> toTarget:<#(id)#> withObject:<#(id)#>]
    [CNGameUtil writeContentToFile:content filePath:localFile];
}
/* 读取本地金币文件内容*/
+(NSString*) readGoldContentFromFile:(NSString*)goldFileName{
     NSString* localFile=[NSString stringWithFormat:@"%@/%@",  [CNGameUtil getLocalGoldPath],goldFileName];
    return [CNGameUtil readContentFromFile:localFile];
}
+(void)removeLocalFile:(NSString*)filePath{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:filePath]){
        NSError* error;
      [fileManager removeItemAtPath:filePath error:&error];
    }
    fileManager=nil;
}

//删除本地金币文件
+(void)removeLocalGoldFile:(NSString*)goldFileName{
     NSString* localFile=[NSString stringWithFormat:@"%@/%@",  [CNGameUtil getLocalGoldPath],goldFileName];
    [CNGameUtil removeLocalFile:localFile];
    localFile=nil;
}
//获得等级数据
+(NSArray*)getAllPlayerLevel{
    
    return [NSArray arrayWithObjects:[CNGoldCoin initWith:@"屌丝" coins:@"1000"]
                    ,[CNGoldCoin initWith:@"短工" coins:@"1500"],[CNGoldCoin initWith:@"长工" coins:@"2500"],[CNGoldCoin initWith:@"佃户" coins:@"4000"],[CNGoldCoin initWith:@"贫农" coins:@"8000"],[CNGoldCoin initWith:@"渔夫" coins:@"14000"],[CNGoldCoin initWith:@"猎人" coins:@"23000"],[CNGoldCoin initWith:@"中农" coins:@"36500"],[CNGoldCoin initWith:@"富农" coins:@"50000"],[CNGoldCoin initWith:@"掌柜" coins:@"70000"],[CNGoldCoin initWith:@"商人" coins:@"100000"],[CNGoldCoin initWith:@"衙役" coins:@"150000"],[CNGoldCoin initWith:@"小财主" coins:@"220000"],[CNGoldCoin initWith:@"大财主" coins:@"300000"],[CNGoldCoin initWith:@"小地主" coins:@"400000"],[CNGoldCoin initWith:@"大地主" coins:@"550000"],[CNGoldCoin initWith:@"知县" coins:@"770000"],[CNGoldCoin initWith:@"通判" coins:@"1000000"],[CNGoldCoin initWith:@"知府" coins:@"1400000"],[CNGoldCoin initWith:@"总督" coins:@"2000000"],[CNGoldCoin initWith:@"巡抚" coins:@"3000000"],[CNGoldCoin initWith:@"丞相" coins:@"4500000"],[CNGoldCoin initWith:@"帝王" coins:@"7000000"], nil];
}
//计算金币所处等级
+(NSString*)calcPlayerLevelName:(int)coins{
    NSString* levelName=@"帝王";
    for (CNGoldCoin* coin in [CNGameUtil getAllPlayerLevel]) {
        if([coin.coins intValue]>=coins){
            levelName=coin.levelName;
            break;
        }
    }
    return levelName;
}
@end
